package hhm.android.main

import android.os.Bundle
import android.text.TextUtils
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import com.trello.rxlifecycle4.RxLifecycle
import com.trello.rxlifecycle4.android.ActivityEvent
import hhm.android.library.datePicker.bean.DateParams
import hhm.android.library.utils.DialogUtils
import hhm.android.library.utils.GetDateListener
import hhm.android.library.utils.GetStringListener
import hhm.android.library.weightHeightPicker.weightPicker.WeightParams
import hhm.android.main.databinding.ActivityInputWeightByHandBinding
import siau.android.base.HttpHelper
import siau.android.base.SBApplication
import siau.android.base.SBBaseActivity
import siau.android.base.enum.WeightClass
import siau.android.http.model.RecordWeightDataRequest
import siau.android.library.KeyString
import java.text.SimpleDateFormat
import java.util.*

class InputWeightByHandActivity : SBBaseActivity() {

    lateinit var dataBinding: ActivityInputWeightByHandBinding
    private var childId: Int = -1
    private var timeChange = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTitleWhiteAndTextBlank()
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_input_weight_by_hand)
        initTitleWithLine(getString(R.string.input_by_hand))
        dataBinding.lifecycleOwner = this

        childId = intent.getIntExtra(KeyString.childId, -1)
        var oldData = intent.getDoubleExtra(KeyString.data, 0.0)

        var cla = ""
        val dateParams = WeightParams()
        dateParams.types = IntArray(2)
        when (SBApplication.userData.weightUnit) {
            WeightClass.KG.msg -> {
                dateParams.types[0] = WeightParams.TYPE_KG
                dateParams.types[1] = WeightParams.TYPE_MG

                if (oldData.isNaN() || oldData == 0.0) {
                    oldData = 3.00
                    dataBinding.activityInputWeightByHandTv.text = "3.00"
                } else {
                    dataBinding.activityInputWeightByHandTv.text = oldData.toString()
                }
                cla = getString(R.string.class_kg)
            }
            WeightClass.JIN.msg -> {
                dateParams.types[0] = WeightParams.TYPE_JIN
                dateParams.types[1] = WeightParams.TYPE_LIANG
                if (oldData.isNaN() || oldData == 0.0) {
                    oldData = 6.00
                    dataBinding.activityInputWeightByHandTv.text = "6.00"
                } else {
                    dataBinding.activityInputWeightByHandTv.text = oldData.toString()
                }
                cla = getString(R.string.class_jin)
            }
            WeightClass.LB.msg -> {
                dateParams.types[0] = WeightParams.TYPE_LB
                dateParams.types[1] = WeightParams.TYPE_OZ
                if (oldData.isNaN() || oldData == 0.0) {
                    oldData = 6.0
                    dataBinding.activityInputWeightByHandTv.text = "6.0"
                } else {
                    dataBinding.activityInputWeightByHandTv.text = oldData.toString()
                }
                cla = getString(R.string.class_bang)
            }
        }

        dataBinding.activityInputWeightByHandClass.text = cla

        dataBinding.wheelLayout.show(SBApplication.userData.weightUnit, oldData, dateParams, ContextCompat.getColor(this, R.color.mainTextColor))
        dataBinding.wheelLayout.setOnChangeListener(object : GetStringListener {
            override fun getString(s: String) {
                dataBinding.activityInputWeightByHandTv.text = s
            }
        })


        val sdf = SimpleDateFormat("yyyy/MM/dd HH:mm")
        val time = sdf.format(Date())
        val times = time.split(" ")
        dataBinding.activityInputWeightByHandDate.text = times[0]
        dataBinding.activityInputWeightByHandTime.text = times[1]

        dataBinding.activityInputWeightByHandSelectDate.setOnClickListener {
            DialogUtils.showDatePicBottomView(
                this,
                object : GetDateListener {
                    override fun getDate(date: Date, string: String) {
                        dataBinding.activityInputWeightByHandDate.text = string
                    }
                },
                "yyyy/MM/dd",
                DateParams.TYPE_YEAR, DateParams.TYPE_MONTH, DateParams.TYPE_DAY,
            )
        }
        dataBinding.activityInputWeightByHandSelectTime.setOnClickListener {
            DialogUtils.showDatePicBottomView(
                this, object : GetDateListener {
                    override fun getDate(date: Date, string: String) {
                        timeChange = true
                        dataBinding.activityInputWeightByHandTime.text = string
                    }
                }, "HH:mm", DateParams.TYPE_HOUR, DateParams.TYPE_MINUTE
            )
        }
        dataBinding.activityInputWeightByHandBtn.setOnClickListener {
            showLoadingFragment(R.id.activity_input_weight_by_hand_fl)
            val sdf = SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
            val time = sdf.format(Date())
            val times = time.split(" ")
            val ss = times[1].split(":")
            var d = if (timeChange) {
                sdf.parse(dataBinding.activityInputWeightByHandDate.text.toString() + " " + dataBinding.activityInputWeightByHandTime.text.toString() + ":" + ss[2])
            } else {
                sdf.parse(dataBinding.activityInputWeightByHandDate.text.toString() + " " + times[1])
            }

            val r = RecordWeightDataRequest(childId, d.time, 2, dataBinding.activityInputWeightByHandTv.text.toString(), weightUnit = SBApplication.userData.weightUnit)
            HttpHelper().recordWeight(r)?.compose(RxLifecycle.bindUntilEvent(lifecycle(), ActivityEvent.STOP))?.subscribe({
                removeLoadingFragment()
                setResult(1001)
                finish()
            }, {
                removeLoadingFragment()
                showToast(it.message)
            })
        }
    }
}